package com.pan.hisystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingyang
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/outpatients")
public class OutpatientsController {

    @Autowired
    private IOutpatientsService outpatientsService;
    @Autowired
    private IDrugService drugService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRegistrationService registrationService;
    @Autowired
    private IPrescriptionDrugsService prescriptionDurgsService;

    @Autowired
    private IMedicalCardService medicalCardService;
    @Autowired
    private IPatientService patientService;

    @Autowired
    private ITestService testService;
    @Autowired
    private IPatientTestService patientTestService;

    @Autowired
    private ITestImageService testImageService;
    @Autowired
    private IDepartmentService departmentService;


    @ApiOperation(value = "根据卡号获取患者信息")
    @GetMapping("/getMessageByMid/{mid}")
    public RespBean getMessageByMid(@PathVariable String mid, Principal principal){
        if(principal==null){
            return RespBean.error("操作员未登录,请登录!");
        }
        User user =userService.getOne(new QueryWrapper<User>().eq("username",principal.getName()));
        return outpatientsService.getMessageByMid(mid, user.getId());
    }

    @ApiOperation(value = "获取药品")
    @GetMapping("/getDrugs")
    public List<Drug> getDrugs(){
        return drugService.list();
    }

    @ApiOperation(value = "获取患者就诊记录")
    @GetMapping("/getOutPatientByMid/{mid}")
    public RespBean getOutPatientByMid(@PathVariable String mid,Principal principal){
        if(principal==null){
            return RespBean.error("操作员未登录,请登录!");
        }
        User user =userService.getOne(new QueryWrapper<User>().eq("username",principal.getName()));
        Registration registration=registrationService.getOne(new QueryWrapper<Registration>()
                .eq("mid",mid)
                .eq("uid", user.getId())
                .eq("back_status",false)
                .eq("enabled",false));
        //挂号id跟就诊号一对一
        Outpatients outpatients=outpatientsService.getOne(new QueryWrapper<Outpatients>()
                .eq("rid",registration.getId())
                .eq("enabled",false));//未完成就诊
        if(outpatients!=null){
            return RespBean.success("获取成功!",outpatients);
        }
        return RespBean.error("无即将就诊记录,请先挂号!");
    }

    @ApiOperation(value = "更新就诊记录")
    @PutMapping("/updateOutpatients")
    public RespBean updateOutpatients(@RequestBody Outpatients outpatients){
        return outpatientsService.updateOutpatients(outpatients);
    }

    @ApiOperation(value = "开处方药")
    @PostMapping("/addDrug")
    public RespBean addDrug(@RequestBody PrescriptionDrugs prescriptionDrugs){
        if(prescriptionDrugs ==null){
            return RespBean.error("添加失败!");
        }
        if(prescriptionDrugs.getPreId()==null){
            return RespBean.error("处方号不能为空,请先获取处方号!");
        }
        if(null==outpatientsService.getOne(new QueryWrapper<Outpatients>().eq("enabled",false).eq("preId",prescriptionDrugs.getPreId()))){
            return RespBean.error("该门诊已完成就诊!");
        }
        prescriptionDrugs.setStatus(false);
        if(prescriptionDurgsService.save(prescriptionDrugs)){
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "获取某医生下的所有就诊记录")
    @GetMapping("/getAllOutpatientsRecords")
    public List<Outpatients> getAllOutpatientsRecords(Authentication authentication){
            User user =(User)authentication.getPrincipal();
            System.out.println(user.toString());
            return outpatientsService.list(new QueryWrapper<Outpatients>().eq("uid", user.getId()));
    }

    @ApiOperation(value = "根据mid获取某医生下就诊记录")
    @GetMapping("/getRecords")
    public List<Outpatients> getRecords(String mid, Authentication authentication){
        User user =(User)authentication.getPrincipal();
        System.out.println(user.toString());
        return outpatientsService.list(new QueryWrapper<Outpatients>().eq("mid",mid).eq("uid", user.getId()));
    }

    @ApiOperation(value = "根据名称、功效、类型获取药品")
    @PutMapping("/getDrugsByType")
    public List<Drug> getDrugsByType(@RequestBody Drug drug){
        System.out.println(drug.toString());
        return drugService.getDrugsByType(drug);
    }

    @ApiOperation(value = "获取化验项目")
    @GetMapping("/getTests")
    public List<Test> getTests(){
        return testService.list();
    }

    @ApiOperation(value = "开化验单")
    @GetMapping("/addTest")
    public RespBean addTest(Integer id,Integer oid){
        return patientTestService.addTest(id,oid);
    }

    @ApiOperation(value = "根据化验id获取化验信息")
    @GetMapping("/getTestMes")
    public RespBean getTestMes(Integer id){
        return patientTestService.getTestMes(id);
    }

    @ApiOperation(value = "根据卡号获取化验结果")
    @GetMapping("/getTestResultByMid")
    public RespBean getTestResultByMid(String mid){
        return patientTestService.getTestResultByMid(mid);
    }

    @ApiOperation(value = "/根据化验号获取化验图片")
    @GetMapping("/getTestImage")
    public List<String> getTestImage(Integer tid){
        List<TestImage> list=testImageService.list(new QueryWrapper<TestImage>().eq("tid",tid));
        List<String> list1=new ArrayList<>();
        for (TestImage testImage : list) {
            list1.add(testImage.getImageURL());
        }
        return list1;
    }

    @ApiOperation(value = "根据用户id获取科室")
    @GetMapping("/getDepByUid")
    public Department getDepByUid(Integer uid){
        return departmentService.getDepByUid(uid);
    }




}
